package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.List;

import qlsl.androiddesign.activity.subactivity.CategoryListActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingListActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingReleaseActivity;
import qlsl.androiddesign.adapter.subadapter.CategoryListAdapter;
import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.entity.otherentity.ShoopChildList;
import qlsl.androiddesign.entity.otherentity.ShoopChildList.RowsBean;
import qlsl.androiddesign.entity.otherentity.ThreeCategory;
import qlsl.androiddesign.http.service.subservice.CategoryService;
import qlsl.androiddesign.manager.ActivityManager;
import qlsl.androiddesign.view.baseview.FunctionView;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.qlsl.androiddesign.kuaiwa.R;

/**
 * 分类页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class CategoryListView extends FunctionView<CategoryListActivity> {

	private ListView listView;

	private List<ThreeCategory.RowsBean> list = new ArrayList<ThreeCategory.RowsBean>();

	public CategoryListView(CategoryListActivity activity) {
		super(activity);
		setContentView(R.layout.activity_category_listview);
		listView.setEmptyView(findViewById(R.id.tv_empty));
	}

	protected void initView(View view) {
		setTitle("分类");
//		setImmersionBarColor(activity.getResources().getColor(R.color.red));

		listView = findViewById(R.id.listView);
		ShoopChildList.RowsBean category = (RowsBean) activity.getIntent()
				.getSerializableExtra("data");
		if (category != null) {
			queryCategoryListChild(category);
		}
	}

	protected void initData() {

	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {
		if (t[0] instanceof List) {
			List<ThreeCategory.RowsBean> list_result = (List<ThreeCategory.RowsBean>) t[0];
			resetList(list_result);
			notifyDataSetChanged();
		}

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.layout_group:
			doClickItemView(view);
			break;

		default:
			break;
		}
	}

	private void resetList(List<ThreeCategory.RowsBean> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
		if (adapter == null) {
			adapter = new CategoryListAdapter(activity, list);
			listView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	public void queryCategoryListChild(ShoopChildList.RowsBean category) {
		CategoryService.queryCatagoryList(category.getSubtierid(), this,
				activity);
	}

	private void doClickItemView(View view) {
		int position = listView.getPositionForView(view);
		ThreeCategory.RowsBean item = list.get(position);

		Bundle bundle = SoftwareApplication.getInstance().getBundle();
		if (bundle != null) {
			boolean isChoose = bundle.getBoolean("ischoose",false);
			SoftwareApplication.getInstance().setBundle(null);
			if (isChoose) {				
				Bundle bundle2 = new Bundle();
				
				bundle2.putSerializable("category", item);
				ActivityManager.getInstance().popToActivity(
						ShoppingReleaseActivity.class, bundle2);
				return;
			}
		}
		startActivity(item, ShoppingListActivity.class);

	}
}
